﻿using B.S.Base.Read.API.Application.Command;
using MediatR;
using WEBAPI.Damoin;
using WEBAPI.ErroeCode;
using WEBAPI.Indaseture.Infasetures;

namespace B.S.Base.Read.API.Application.CommandHandLer
{
    public class GetRoleaccountCommandHandLer : IRequestHandler<GetRoleaccountCommand, APIResult<List<AccountModel>>>
    {
        private readonly IBaseRepostiy<AccountModel> user;
        private readonly IBaseRepostiy<UserRoleModel> userrole;

        public GetRoleaccountCommandHandLer(IBaseRepostiy<AccountModel> user, IBaseRepostiy<UserRoleModel> userrole)
        {
            this.user = user;
            this.userrole = userrole;
        }

        public async Task<APIResult<List<AccountModel>>> Handle(GetRoleaccountCommand request, CancellationToken cancellationToken)
        {
           
            List<int> AccountId = userrole.GetAll().Where(x => x.RoleId == request.RoleId).Select(x => x.AccountId).ToList();
            List<AccountModel> list = new List<AccountModel>();
            foreach (int accountId in AccountId)
            {
                var users = user.GetAll().Where(x => x.AccountId == accountId).ToList();
                foreach (var user in users)
                {
                    list.Add(new AccountModel()
                    {
                        AccountId = user.AccountId,
                        DeptId = user.DeptId,
                        AccountName = user.AccountName,
                        Sex = user.Sex,
                        Province = user.Province,
                        Market = user.Market,
                        County = user.County,
                        Address = user.Address,
                        CreateUserName = user.CreateUserName,
                        CreateUserTime = user.CreateUserTime,
                        UpdateUserName = user.UpdateUserName,
                        UpdateUserTime = user.UpdateUserTime,
                        IsDeleted = user.IsDeleted
                    });
                }
            }
            return new APIResult<List<AccountModel>>
            {
                Code = ResultCode.Ok,
                Data = list.ToList(),
                Message="获取成功",
            };
        }
    }
}
